<?php
	class LevelAction extends BaseAction{
		public function index(){
			$level = M("Level");
			import("ORG.Util.Page");
			$count = $level->count();
			$page = new Page($count,c("PAGESIZE"));
			$show = $page->show();
			$data = $level->field(true)
					->limit($page->firstRow.','.$page->listRows)
					->select();
			$this->assign('level',$data);
			$this->assign('page',$show);
			$this->display();
		}
		
		public function add(){
			$permission = M("Permission");
			$data = $permission->field('id,name')
								->select();
			$this->assign('permission',$data);
			$this->display();
		}
		
		public function insert(){
			$level = D("Level");
			$permission = implode(",", $_POST['permission']);
			if(null == $permission)
				$this->error("请至少选择一项权限！");
			if($data = $level->create()){
				$data['permission'] = $permission;
				if($level->add($data)){
					$this->success("添加等级成功！");
				}else{
					$this->error("添加等级失败！");
				}
			}else{
				$this->error($level->getError());
			}
		}
		
		public function edit(){
			if(!empty($_GET['id'])){
				$level = M('Level');
				$data = $level->field('id,level_name,level_info,permission')
				->getById($_GET['id']);
				if(null == $data)
					$this->error("等级ID非法！");
				$this->assign('level',$data);
				$permission = M('permission');
				$this->assign('permission',$permission->field('id,name')->select());
				$this->display();
			}else{
				$this->error("请选择要修改的等级！");
			}

		}
		
		public function update(){
			$level = D('Level');
			if($data = $level->create()){
				$data['permission'] = implode(',', $_POST['permission']);
				if(null == $data['permission'])
					$this->error("请至少选择一项权限！");
				if($level->save($data)){
					$this->success("修改等级成功！");
				}else {
					$this->error("数据没变化过修改等级失败！");
				}
			}else{
				$this->error($level->getError());
			}
		}
		
		public function delete(){
			if(!empty($_GET['id'])){
				$level = M('Level');
				if($level->delete($_GET['id'])){
					$this->success('删除等级成功！');
				}else{
					$this->error('删除失败，请确认ID合法！');
				}
			}else {
				$this->error('请选择要删除的等级！');
			}
		}

	
	}
?>